package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.park.domain.MaWarehouse;
import com.ruoyi.park.mapper.MaHouseMaterialMapper;
import com.ruoyi.park.mapper.MaWarehouseMapper;
import com.ruoyi.park.service.IMaWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库Service业务层处理
 *
 * @author cjie
 * @date 2021-09-16
 */
@Service
public class MaWarehouseServiceImpl implements IMaWarehouseService {
    @Autowired
    private MaWarehouseMapper maWarehouseMapper;
    @Autowired
    private MaHouseMaterialMapper houseMaterialMapper;

    /**
     * 查询仓库
     *
     * @param id 仓库ID
     * @return 仓库
     */
    @Override
    public MaWarehouse selectMaWarehouseById(String id) {
        return maWarehouseMapper.selectMaWarehouseById(id);
    }

    /**
     * 查询仓库列表
     *
     * @param maWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<MaWarehouse> selectMaWarehouseList(MaWarehouse maWarehouse) {
        return maWarehouseMapper.selectMaWarehouseList(maWarehouse);
    }

    /**
     * 新增仓库
     *
     * @param maWarehouse 仓库
     * @return 结果
     */
    @Override
    public AjaxResult insertMaWarehouse(MaWarehouse maWarehouse) {
        maWarehouse.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        maWarehouse.setCreateTime(DateUtils.getNowDate());
        maWarehouse.setCreateBy(SecurityUtils.getUsername());

        if(UserConstants.NOT_UNIQUE.equals(checkHouseNumUnique(maWarehouse))){
            return AjaxResult.error("当前仓库编号已存在");
        }

        return maWarehouseMapper.insertMaWarehouse(maWarehouse) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 校验仓库编号是否唯一
     * @param maWarehouse
     * @return
     */
    private String checkHouseNumUnique(MaWarehouse maWarehouse) {
        MaWarehouse source = maWarehouseMapper.selectWarehouseByNum(maWarehouse.getHouseNum());

        if(source != null && !source.getId().equals(maWarehouse.getId())){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 修改仓库
     *
     * @param maWarehouse 仓库
     * @return 结果
     */
    @Override
    public AjaxResult updateMaWarehouse(MaWarehouse maWarehouse) {
        maWarehouse.setUpdateTime(DateUtils.getNowDate());
        maWarehouse.setUpdateBy(SecurityUtils.getUsername());

        if(UserConstants.NOT_UNIQUE.equals(checkHouseNumUnique(maWarehouse))){
            return AjaxResult.error("当前仓库编号已存在");
        }
        return maWarehouseMapper.updateMaWarehouse(maWarehouse)  > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除仓库
     *
     * @param ids 需要删除的仓库ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteMaWarehouseByIds(String[] ids) {
        for(String houseId : ids){
            if(houseMaterialMapper.countMaHouseMaterialByHouseId(houseId)>0){
                return AjaxResult.error("仓库存在物资不可删除");
            }

        }
        return maWarehouseMapper.deleteMaWarehouseByIds(ids) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除仓库信息
     *
     * @param id 仓库ID
     * @return 结果
     */
    @Override
    public int deleteMaWarehouseById(String id) {
        return maWarehouseMapper.deleteMaWarehouseById(id);
    }
}
